const myAjax = function (options = {}) {
  // method = 'GET', url, data = null, success, error
  const method = options.method || 'GET'
  const url = options.url
  const data = JSON.stringify(options.data) || null
  const success = options.success
  const error = options.error

  let xhr = new XMLHttpRequest()
  xhr.open(method, url)
  xhr.setRequestHeader('content-type', 'application/json; charset=utf-8')
  xhr.send(data)
  xhr.onreadystatechange = function () {
    if (xhr.readyState === 4) {
      if (xhr.status === 200) {
        const res = JSON.parse(xhr.response)
        success(res)
      } else {
        error()
      }
    }
  }
}
